package com.itshidu.jiaowu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.entity.Teacher;
import com.itshidu.jiaowu.dao.TeacherMapper;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.service.TeacherService;
import com.itshidu.jiaowu.util.LoginUtil;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.web.tools.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08
 */

@Service
@Primary
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired UserMapper userMapper;
    @Autowired TeacherMapper teacherMapper;

    //修改老师信息
    @Override
    public Result updated(Teacher teacher, User user) {


        User loginuser= (User) LoginUtil.getsession().getAttribute("UserInfo");         //获取当前登录的用户信息
        User u =userMapper.selectById(loginuser.getId());                                  //查出当前用户
        u.setUsername(user.getUsername());                                                 //修改用户表各项属性
        u.setPhone(user.getPhone());
        u.setWx(user.getWx());
        u.setSex(user.getSex());
        u.setBirthday(user.getBirthday());
        u.setAddress(user.getAddress());
        u.setQq(user.getQq());
        userMapper.updateById(u);

        Teacher t = teacherMapper.selectById(loginuser.getId());                            //查找出当前老师
        t.setJob(teacher.getJob());                                                         //修改老师表的各项属性
        t.setIntro(teacher.getIntro());
        teacherMapper.updateById(t);
        LoginUtil.set("UserInfo", u);
        LoginUtil.set("Teacher", t);

        return Result.of(200, "修改成功");
    }

    //修改老师密码
    @Override
    public Result ChangePwd(String oldpwd, String newpwd1, String newpwd2) {

        User loginuser= (User) LoginUtil.getsession().getAttribute("UserInfo");         //查找出当前老师
        User u =userMapper.selectById(loginuser.getId());

        if (oldpwd==null){
            Result.of(201);//请填写原密码
        }
        if (newpwd1==null){

            Result.of(202);//请填写新密码
        }
        if (newpwd2==null){
            Result.of(203);//请填写确认密码
        }

        String a= MD5Util.md5(oldpwd+u.getSalt());
        if (!u.getPassword().equals(a)){                                                 //判断原密码是否正确
            return Result.of(200);
        }
        if (!newpwd1.equals(newpwd2)){                                                         //判断两个新密码是否与一致
            return  Result.of(100);
        }

        String newpwd=MD5Util.md5(newpwd2+u.getSalt());
        u.setPassword(newpwd);                                                                //修改密码
        userMapper.updateById(u);
        return Result.of(300);
    }

}
